// pages/cost_detail/cost_detail.js
var app;
var url;
var headers, payInfo;
var rxPhone = /^1[3|4|5|7|8][0-9]{9}$/;
var idReg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/; 
var payInfor = {
  name: ["", false,"",false],//内容 、表单验证、错误信息、是否为空
  phone: ["", false,"",false],
  careType: ["sfz", true,"",true],
  care_no: ["", false,"",false],
  tourist: false,
  checked:[true,"请阅读条款"]
};
var thisflag=true//避免重复提交
var card_sort_=["sfz", "passport", "ga", "tw"];
var information_mes="";
Page({
  data:{
    url:"https://app1.sztravel.com.cn/file/weixinapp/img",
    cost_detail:false,
    index:0,
    card_sort:["身份证","护照","港澳台通行证","台湾通行证"],
    ispay:false,
    cost_use:{},
    fromTestInfo:{},
    select_num:0,
    check_info:[],
    empty:true,
    canPay:false,
    peer_id_list:"",
    checkedFlag:true,
    payInfo:{
      name:"",
      phone: "",
      careType: "",
      care_no: "",
    }
  },
  //证件类型选择
   bindPickerChange: function(e) {
     payInfor.careType[0] = card_sort_[e.detail.value];
    //  console.log(payInfor.careType[0]);
    this.setData({
      index: e.detail.value
    })
  },
//选择游客
  select_tourist(){
    if (!app.globalData.islogin){
      var information = this.data.fromTestInfo;
      // this.saveMes();
      wx.navigateTo({
        url: '../toLogin/toLogin',
      })
      return;
    }
    wx.navigateTo({
      url: '../../pages/select_tourist/select_tourist',
    })
  },
  saveMes:function(){
     payInfo = this.data.payInfo;
    payInfo.name = payInfor.name[0];
    payInfo.phone = payInfor.phone[0];
    payInfo.care_no = payInfor.care_no[0];
    payInfo.careType = this.data.card_sort[this.data.index];
    console.log(payInfo);
    this.setData({
      payInfo: payInfo
    })
    wx.setStorageSync("information_mes", JSON.stringify(payInfo))
  },
  //显示关闭费用明细
  touristchange(e){
    var cdata = this.data.order_info;
    console.log(cdata)
     if(e.target.dataset.adult){
      //成人
      if(e.target.dataset.adult==2){
          cdata.tourist_num.adult--
          if(cdata.tourist_num.adult<0){
            cdata.tourist_num.adult=0
          }else{
            cdata.tourist_num.sum--
          }
        }else{
          cdata.tourist_num.adult++
          cdata.tourist_num.sum++
        }
    }else if(e.target.dataset.children){
      //儿童
        if(e.target.dataset.children==2){
          cdata.tourist_num.children--
          if(cdata.tourist_num.children<0){
            cdata.tourist_num.children=0
          }else{
            cdata.tourist_num.sum--
          }
        }else{
          cdata.tourist_num.children++
          cdata.tourist_num.sum++
        }
    }
    this.setData({
      order_info:cdata
    })
    app.globalData.order_info = cdata
    console.log(cdata)
  },
  detail(e){
    this.setData({
      cost_detail:!this.data.cost_detail
    })
  },
  isEmpty:function(){
    var that=this;
    console.log(payInfor)
    if (payInfor.name[3] && payInfor.phone[3] && payInfor.care_no[3] && payInfor.tourist && payInfor.checked[0]) {
      // console.log(333)
      this.setData({
        empty: false
      })
    }else{
      this.setData({
        empty: true
      })
    }
  },
  read:function (e) {
    var checkedFlag = this.data.checkedFlag
    if (e.detail.value.length > 0) {//勾选阅读协议
      payInfor.checked[0] = true;
      checkedFlag=true;
    } else {
      payInfor.checked[0] = false;
      checkedFlag=false;
    }
    this.setData({
      checkedFlag: checkedFlag
    })
    this.isEmpty();
  },
  connectName:function(e){//输入姓名   
    payInfor.name[0] = e.detail.value;
    if(e.detail.cursor>0){//判空
      payInfor.name[1]=true;
      payInfor.name[2] ="";
        payInfor.name[3]=true
    } else if (e.detail.cursor <=0){
      payInfor.name[1] = false;
      payInfor.name[2]="姓名不可为空";
      payInfor.name[3] = false;
    }
    this.isEmpty();
    // console.log(payInfor.name)
  },
  connectPhone:function(e){
    payInfor.phone[0]=e.detail.value;
    if (e.detail.cursor>0){
      payInfor.phone[3]=true
    }else{
      payInfor.phone[3] = false
    }
    if (!rxPhone.test(e.detail.value)){
      payInfor.phone[2]="手机号码格式有误";
      payInfor.phone[1]=false;
    }else{
      payInfor.phone[2] = "";
      payInfor.phone[1] = true;
    }
    console.log(payInfor.phone)
    this.isEmpty();
  },
  connectCardType:function(e){   
    payInfor.care_no[0] = e.detail.value;
    if (e.detail.cursor>0){//目前只有判空和长度不大于18规则
      payInfor.care_no[1]=true;
      payInfor.care_no[2] ="";
      payInfor.care_no[3] = true;

    }else{
      payInfor.care_no[1] = false;
      payInfor.care_no[2] = "证件号码不能为空";
      payInfor.care_no[3] = false;
    }
    this.isEmpty();
  },
  showMode:function(mes){
      wx.showModal({
        title: '来自伴行的提示',
        content: mes,
        showCancel:false
      })
  },
  //支付页面显示
  submit:function(){
    var that=this;
    // var payInfor = {
    //   name: ["", false, "", false],//内容 、表单验证、错误信息、是否为空
    //   phone: ["", false, "", false],
    //   careType: ["sfz", true, "", true],
    //   care_no: ["", false, "", false],
    //   tourist: false,
    //   checked: [true, "请阅读条款"]
    // };
    console.log(this.data);
      var empty=this.data.empty;//判空正确
      
      if (empty){//为空
        return
      }else{//表单验证
        if (!payInfor.name[1]){
          that.showMode(payInfor.name[2]);
          return;
        } else if (!payInfor.phone[1]){
          that.showMode(payInfor.phone[2]);
          return;
        } else if (!payInfor.care_no[1]){
          that.showMode(payInfor.care_no[2]);
          return;
        }else if(!thisflag){//不可重复提交
          that.showMode("订单已提交请耐心等待");
          return;
        } else if (payInfor.careType[0] == "sfz" && !idReg.test(payInfor.care_no[0])){
          that.showMode("身份证号码有误");
          return; 
        }else{//可以提交
          thisflag=false
          var fromTestInfo= that.data.fromTestInfo;
          // var payInfor = {
          //   name: ["", false, "", false],//内容 、表单验证、错误信息、是否为空
          //   phone: ["", false, "", false],
          //   careType: ["sfz", true, "", true],
          //   care_no: ["", false, "", false],
          //   tourist: false,
          //   checked: [true, "请阅读条款"]
          // };
          var req = '{"team_id":'
            + fromTestInfo.teem_id + ',"adult_num":'
            + fromTestInfo.adult_num + ',"child_num":'
            + fromTestInfo.child_num + ',"adult_price":'
            + (fromTestInfo.adult_price).toFixed(2) + ',"child_price":'
            + (fromTestInfo.child_price).toFixed(2) + ',"total_price":'
            + parseFloat(fromTestInfo.total_price).toFixed(2) + ',"peer_id_list":"'
            + that.data.peer_id_list + '","names":"'
            + payInfor.name[0] + '","phone_num":"'
            + payInfor.phone[0] + '","card_type":"'
            + payInfor.careType[0] + '","card_no":"'
            + payInfor.care_no[0] + '"}';
            console.log(req);
          wx.request({
            url: url+'api/booking_order',
            method:"POST",
            header:headers,
            data:{
              "req":encodeURIComponent(req)
            },
            success:function(data){
              thisflag = true;
              console.log(data)
              wx.showModal({
                title: '来自伴行的提示',
                content:data.data.message,
                showCancel:false,
                success:function(){
                  if(data.data.result===1){//提交成功
                    var orderMes={
                      title: that.data.cost_use.title,
                      time_start: that.data.fromTestInfo.date,
                      depart: that.data.cost_use.depart,
                      total_num: that.data.fromTestInfo.total_num,
                      total_price: that.data.fromTestInfo.total_price,
                      order_no: data.data.data.order_no
                    }
                    console.log(orderMes)
                    try{
                      wx.setStorageSync("orderMes", JSON.stringify(orderMes))
                    }catch(e){

                    }
                    wx.navigateTo({
                      url: '../pay/pay',
                    })
                  }
                }
              })
              
            },
            fail:function(data){
              console.log(data)
            }
          })
        }
      }
    
  },
  onLoad:function(options){
    if(options.from){
      information_mes = options.from
    }
    // if(options.order_info){
    //   this.setData({
    //     order_info:JSON.parse(options.order_info),
    //   })
    //   app.globalData.order_info=JSON.parse(options.order_info)
    // }
    // if(options.tourist){
    //   this.setData({
    //      tourist:JSON.parse(options.tourist)
    //   })
    //   app.globalData.tourist=JSON.parse(options.tourist)
    // }
  // console.log(app.globalData)
  console.log(this.data)
  },
  onReady:function(){
    // 页面渲染完成
  },
  onShow:function(){
    app = getApp();
    url = app.globalData.url
    headers = app.globalData.headers;
    var fromTestInfo, costDetailUse, check_info=null, length;
    var that=this
    //页面加载完成
    //接受订单信息或者请求订单信息
    try {
      fromTestInfo = JSON.parse(wx.getStorageSync("test_info"));
      costDetailUse = JSON.parse(wx.getStorageSync("costDetailUse"));
      check_info = JSON.parse(wx.getStorageSync("check_info"));
      
      console.log(fromTestInfo);
      console.log(costDetailUse);
    } catch (e) {
    };
    if (information_mes){//从登陆页面跳过来
  
      var infor = JSON.parse(wx.getStorageSync("information_mes"));
      that.setData({
        payInfo:infor
      })
    }
    if (check_info){//带回来了游客信息
      length = check_info.length;
      payInfor.tourist=true;
      var str=""
      for (let j = 0; j < check_info.length;j++){
        str += check_info[j].peer_id+","
      }
      str=str.substring(0,str.length-1);
      that.isEmpty();
    }else{
      length=0
    }
    this.setData({
      tourist:app.globalData.tourist,
      order_info:app.globalData.order_info,
      cost_use: costDetailUse,
      fromTestInfo: fromTestInfo,
      ispay: false,
      check_info: check_info,
      select_num: length,
      peer_id_list:str
    })
    // 页面显示
    console.log(this.data)
  },
  onHide:function(){
    // 页面隐藏
  },
  onUnload:function(){
    // 页面关闭
  }
})